package com.umeox.iqibla.azkar.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.LayoutDirection;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.text.TextUtilsCompat;
import androidx.viewpager2.widget.ViewPager2;

import java.util.Locale;


public class CirclePageIndicator extends View {

    //点的半径
    private final int mDotRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
    private final int mSelectDotRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
    //点与点的间隔
    private final int mDotGap = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());


    private ViewPager2 mViewPager;
    private Paint mDotPaint;

    private int mPosition;
    private float mPositionOffset;

    //不动点的颜色
    private int mNormalColor;
    //动点颜色
    private int mSelectedColor;

    public int getItemCount() {
        return mItemCount;
    }

    public void setItemCount(int itemCount) {
        mItemCount = itemCount;
    }

    private int mItemCount = 2;

    public CirclePageIndicator(Context context) {
        this(context, null);
    }

    public CirclePageIndicator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mDotPaint = new Paint();
        mDotPaint.setAntiAlias(true);
        mNormalColor = 0XFF6B6B6B;
        mSelectedColor = 0xFFFFFFFF;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = 2 * mDotRadius * mItemCount + (mItemCount - 1) * mDotGap + 2 * mSelectDotRadius;
        int height = 2 * mSelectDotRadius;
        setMeasuredDimension(width, height);
    }

    public void setViewPager(ViewPager2 viewPager) {
        mViewPager = viewPager;

        mViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                mPosition = position;
                mPositionOffset = positionOffset;
                invalidate();
            }

        });
    }


    @Override
    protected void onDraw(Canvas canvas) {
        //点与点之间圆心的距离
        int dotDistance = mDotGap + 2 * mDotRadius;
        int selectDotDistance = mDotGap + 2 * mSelectDotRadius;
        //循环遍历不动点

        for (int i = 0; i < mItemCount; i++) {
            float cx = mSelectDotRadius + i * dotDistance;
            float cy = mSelectDotRadius;
            mDotPaint.setColor(mNormalColor);
            canvas.drawCircle(cx, cy, mDotRadius, mDotPaint);
        }
        //绘制动点
        mDotPaint.setColor(mSelectedColor);
        //计算动点x轴的位置
        float mMoveCx = 0f;
        if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL) {
            mMoveCx = mSelectDotRadius + (mItemCount - 1) * dotDistance - dotDistance * mPositionOffset - mPosition * dotDistance;
        } else {
            mMoveCx = mSelectDotRadius + dotDistance * mPositionOffset + mPosition * dotDistance;
        }
        float mMoveCy = mSelectDotRadius;
        canvas.drawCircle(mMoveCx, mMoveCy, mSelectDotRadius, mDotPaint);
    }
}